package com.flag.liutao.modules.student.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flag.liutao.common.handler.MyException;
import com.flag.liutao.modules.student.domain.Department;
import com.flag.liutao.modules.student.mapper.DepartmentMapper;
import com.flag.liutao.modules.student.param.DeptParam;
import com.flag.liutao.modules.student.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flag.liutao.modules.student.vo.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author godM
 * @since 2022-12-06
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 获取学院列表
     * @param page
     * @param limit
     * @param keyword
     * @return
     */
    @Override
    public Map<String, Object> getDeptPage(int page, int limit, String keyword) {
        IPage<Department> iPage = new Page<>(page,limit);
        LambdaQueryWrapper<Department> lqw = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(keyword))lqw.like(Department::getDeptName,keyword);
        IPage<Department> pageDB = departmentMapper.selectPage(iPage, lqw);
        Map<String,Object> map = new HashMap<>();
        List<DepartmentVo> list = Convert.convert(new TypeReference<List<DepartmentVo>>() {}, pageDB.getRecords());
        map.put("content",list);
        map.put("total",pageDB.getTotal());
        return map;
    }

    /**
     * 添加学院信息
     * @param param
     */
    @Override
    public void addDept(DeptParam param) {
        LambdaQueryWrapper<Department> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Department::getDeptName,param.getDeptName());
        Department department = departmentMapper.selectOne(lqw);
        if(ObjectUtil.isNotNull(department)){
            throw new MyException("二级学院"+param.getDeptName()+"已存在");
        }
        Department dept = new Department();
        dept.setDeptName(param.getDeptName());
        departmentMapper.insert(dept);
    }

    /**
     * 修改学院信息
     * @param param
     */
    @Override
    public void editDept(DeptParam param) {
        Department department = departmentMapper.selectById(param.getId());
        if(ObjectUtil.isNull(department)){
            throw new MyException("二级学院不存在");
        }
        department.setDeptName(param.getDeptName());
        departmentMapper.updateById(department);
    }

    /**
     * 删除二级学院
     * @param id
     */
    @Override
    public void delDept(Long id) {
        departmentMapper.deleteById(id);
    }

    /**
     * 获取学院0->学院，1->专业，2->班级列表
     * @return
     */
    @Override
    public List<DepartmentVo> getDeptList(int type) {
        List <DepartmentVo> list = new ArrayList<>();
        switch (type) {
            case 0:
                list = departmentMapper.getListToDept();
                break;
            case 1:
                list = departmentMapper.getListToMajor();
                break;
            case 2:
                list = departmentMapper.getListToClasses();
        }
        return list;
    }

    @Override
    public List<DepartmentVo> getList() {
        return getDeptList(0);
    }

}
